package com.lcifn.middleware.rpc.hello;

import com.lcifn.middleware.rpc.NameService;
import com.lcifn.middleware.rpc.RpcAccessPoint;
import com.lcifn.middleware.rpc.spi.ServiceSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Closeable;
import java.net.URI;

/**
 * Hello服务服务端
 * @author lichao
 * @date 2021/2/1 15:20
 */
public class HelloRpcServer {

    private static final Logger logger = LoggerFactory.getLogger(HelloRpcServer.class);

    public static void main(String[] args) {
        String helloServiceName = HelloService.class.getCanonicalName();
        HelloService helloService = new HelloServiceImpl();
        // 0.启动服务监听
        RpcAccessPoint rap = ServiceSupport.load(RpcAccessPoint.class);
        try (Closeable closeable = rap.startServer()) {
            logger.info("启动服务成功...");
            // 1.添加HelloService服务
            URI helloServiceUri = rap.addServiceProvider(HelloService.class, helloService);
            logger.info("注册服务 {} 到本地服务...", helloServiceName);
            // 2.获取Name服务
            String redisNameServiceUri = "redis://192.168.2.18:6379";
            URI nameServiceUri = URI.create(redisNameServiceUri);
            NameService nameService = rap.getNameService(nameServiceUri);
            // 3.注册HelloService到Name服务
            nameService.registerService(helloServiceName, helloServiceUri);
            logger.info("注册服务 {} 到NameService {}...", helloServiceName, nameServiceUri);

            System.in.read();
        } catch (Exception e) {
            logger.error("服务启动失败", e);
        }

    }
}
